#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'lrqrun'
__mtime__ = '15-11-11'
"""
from utils import JsonResponse
# from staff.models import


def get_staff(req):
    jobfamily = req.REQUEST.get('jobfamily')
    if not jobfamily:
        return JsonResponse({'msg': "'JobFamily' is required.", 'code': 0})
    JobFamily1 = JobFamily.objects.filter(Is_Active=True)
    Family_Codes = list(set(JobFamily1.values_list('Family_Code', flat=True)))
    jobfamilys = jobfamily.split(',')
    datas =[]
    jobfamilys.sort()
    for Job_Family in jobfamilys:
        if Job_Family not in Family_Codes:
            return JsonResponse({'msg': Job_Family + " is unknown JobFamily Code", 'code': 0})
        JobFamily2 = JobFamily.objects.filter(Family_Code=Job_Family)
        staffs = list(set(JobInfo.objects.filter(End_Date=None, Job_Family=JobFamily2).values_list('Basic_Info', flat=True)))
        data = []
        for i in staffs:
            basicinfo = BasicInfo.objects.get(id=i)
            rtn = {'EmployeeCode': basicinfo.Employee_ID, 'ChineseName': basicinfo.Chinese_Name,
                   'WorkEmail': basicinfo.Work_Email, 'OtherEmail': basicinfo.Other_Email}
            data.append(rtn)
        data = sorted(data ,key = lambda x:x['EmployeeCode'])
        staff = {'Family_Code': JobFamily2[0].Family_Code, 'Family_Name': JobFamily2[0].Family_Name, 'list': data}
        datas.append(staff)
    return JsonResponse({'code': 1, 'msg': 'success', 'staffs': datas, 'JobFamily': str(jobfamilys)})